package com.example;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.List;

public class StudentScheduleController implements StudentSubController {

    @FXML
    private TableView<Schedule> scheduleTableView;
    @FXML
    private TableColumn<Schedule, String> courseNameColumn;
    @FXML
    private TableColumn<Schedule, String> dayOfWeekColumn;
    @FXML
    private TableColumn<Schedule, String> startTimeColumn;
    @FXML
    private TableColumn<Schedule, String> endTimeColumn;
    @FXML
    private TableColumn<Schedule, String> locationColumn;
    @FXML
    private TableColumn<Schedule, String> teacherNameColumn;
    @FXML
    private TableColumn<Schedule, String> semesterColumn;

    private StudentController studentController;
    private User currentUser;
    private ScheduleDAO scheduleDAO;

    @Override
    public void setPrimaryStage(javafx.stage.Stage primaryStage) {
        // 不需要在这里设置primaryStage，由主控制器处理
    }

    @Override
    public void setStudentController(StudentController studentController) {
        this.studentController = studentController;
    }

    public void setCurrentUser(User user) {
        this.currentUser = user;
        System.out.println("StudentScheduleController: setCurrentUser called with user: " + (user != null ? user.getUsername() : "null"));
        // 在设置完 currentUser 后加载数据
        loadScheduleData();
    }

    @FXML
    public void initialize() {
        // 初始化 DAO
        scheduleDAO = new ScheduleDAO();

        // 设置表格列与 Schedule 对象的属性关联
        courseNameColumn.setCellValueFactory(new PropertyValueFactory<>("courseName"));
        dayOfWeekColumn.setCellValueFactory(new PropertyValueFactory<>("dayOfWeek"));
        startTimeColumn.setCellValueFactory(new PropertyValueFactory<>("startTime"));
        endTimeColumn.setCellValueFactory(new PropertyValueFactory<>("endTime"));
        locationColumn.setCellValueFactory(new PropertyValueFactory<>("location"));
        teacherNameColumn.setCellValueFactory(new PropertyValueFactory<>("teacherName"));
        semesterColumn.setCellValueFactory(new PropertyValueFactory<>("semester"));

        // 表格数据将在 setCurrentUser 后加载
    }

    private void loadScheduleData() {
        if (currentUser != null && scheduleDAO != null) {
            List<Schedule> schedules = scheduleDAO.getSchedulesByStudentUsername(currentUser.getUsername());
            ObservableList<Schedule> observableSchedules = FXCollections.observableArrayList(schedules);
            scheduleTableView.setItems(observableSchedules);
            System.out.println("Loaded " + schedules.size() + " schedules for user " + currentUser.getUsername());
        } else {
            System.err.println("currentUser or scheduleDAO is null. Cannot load schedule data.");
        }
    }
} 